package com.pro.controller;

import com.pro.pojo.JsCourse;
import com.pro.pojo.JsMonthCount;
import com.pro.pojo.JsonData;
import com.pro.service.JsCourseService;
import com.pro.utils.FastDFSClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author cyj
 */
@RestController
@RequestMapping("/courseTable")
public class JsCourseController {
    private String url=null;
    private String course_url=null;
    @Autowired
    private JsCourseService service;
    @Autowired
    private FastDFSClientUtil dfsClient;
    //课程表
    @RequestMapping("/getTable")
    public JsonData getTable(JsCourse jsCourse){
        return service.getTable(jsCourse);
    }
    //审核记录表
    @RequestMapping("/getTable2")
    public JsonData getTable2(JsCourse jsCourse){
        return service.getTable2(jsCourse);
    }
    //详细条件检索
    @RequestMapping("/getTable3")
    public JsonData getTable3(JsCourse jsCourse){
        return service.getTable3(jsCourse);
    }
    //获取操作记录
    @RequestMapping("/getLog")
    public JsonData getLog(String userName,Integer type,String tableName,String createDate,Integer userId){
        return service.getLog(userName,type,tableName,createDate,userId);
    }
    @RequestMapping("/deleteLog")
    public JsonData deleteLog(Integer id){
        Boolean flag=service.delLog(id);
        return flag!=false?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }
    //获取课程的详细信息
    @RequestMapping("/getCourse")
    public JsonData getProduct(Integer courseId,Integer updateId){
        JsCourse js=service.getCourse(courseId,updateId);
        return js!=null?JsonData.buildSuccess(js):JsonData.buildError("服务端错误");
    }

    //对课程的相关信息进行删除
    @RequestMapping("/delCourse")
    public JsonData delCourse(Integer id,Integer updateId){
        Boolean flag=service.delCourse(id,updateId);
        return flag!=false?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }

    //获取课程的日志
    @RequestMapping("/getCoursejournal")
    @ResponseBody
    public JsonData getCoursejournal(Integer courseId){
        List<JsCourse> flag=service.getCoursejournal(courseId);
        System.err.println(flag);
        return flag!=null?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }

    //获取课程的日志
    @RequestMapping("/getAllJournal")
    @ResponseBody
    public JsonData getAllJournal(Integer courseId,String courseName,String updateDate){
        List<JsCourse> flag=service.getAllJournal(courseId,courseName,updateDate);
        System.err.println(flag);
        return flag!=null?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }

    //根据按钮更新状态
    @RequestMapping("/updateCourseStatus")
    public JsonData updateCourseStatus(Integer id,String courseStatus){
        JsonData js=new JsonData();
        Boolean ret=service.updateCourseStatus(courseStatus,id);
        return ret!=false?JsonData.buildSuccess(ret):JsonData.buildError("服务端错误");
    }

    //获取课程的审核详情
    @RequestMapping("/getRecord")
    @ResponseBody
    public JsonData getRecord(Integer courseId,Integer aduitLogType,String userName,String createDate){
        List<JsCourse> flag=service.getRecord(courseId,aduitLogType,userName,createDate);
        System.err.println(flag);
        return flag!=null?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }

    //修改课程
    @RequestMapping("/updateCourse")
    public JsonData updateCourse(JsCourse jsProduct){
        if(url!=null){
            jsProduct.setUrl(url);
        }
        if(course_url!=null){
            jsProduct.setCourseUrl(course_url);
        }
        Boolean flag=service.updateCourse(jsProduct);
        return flag!=false?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }

    //审核课程
    @RequestMapping("/updateCstatus")
    public JsonData updateCstatus(JsCourse jsProduct){
        String result = jsProduct.getCourseCstatus();
        if(result=="yes"){
            jsProduct.setCourseCstatus("4");
        }else if(result=="no"){
            jsProduct.setCourseCstatus("6");
        }
        Boolean flag=service.updateCstatus(jsProduct);
        return flag!=false?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }
    //添加审核记录
    @RequestMapping("/addRecord")
    public JsonData addRecord(JsCourse jsProduct){
        System.err.println(jsProduct.getCourseId());
        Boolean aBoolean = service.addRecord(jsProduct);
        return aBoolean!=false?JsonData.buildSuccess(aBoolean):JsonData.buildError("服务端错误");
    }

    //添加课程
    @RequestMapping("/addCourse")
    public JsonData addCourse(JsCourse jsCourse){
        System.err.println(jsCourse);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date dateTime = null;
        try {
            dateTime = df.parse(df.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        jsCourse.setCreateDate(dateTime);
        jsCourse.setUrl(url);
        jsCourse.setCourseUrl(course_url);
        System.err.println(jsCourse);
        Boolean bBoolean = service.addCourse(jsCourse);
        return bBoolean!=false?JsonData.buildSuccess(bBoolean):JsonData.buildError("服务端错误");
    }



    //上传图片
    private static int i = 0;
    @RequestMapping(value = "/upFile", method = RequestMethod.POST)
    @ResponseBody
    public JsonData upload(MultipartFile[] files, HttpSession session) {
        String resAccessUrl =null;
        try {
            for (MultipartFile file : files) {
                String fileUrl = dfsClient.uploadFile(file);
                //路径
                resAccessUrl = dfsClient.getResAccessUrl(fileUrl);
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("path",resAccessUrl);
        url=resAccessUrl;
        return map!=null?JsonData.buildSuccess(map):JsonData.buildError("服务端错误");
    }

    @RequestMapping("/uploadFile")
    public JsonData mp4Upload(@RequestParam("files") MultipartFile[] files, HttpServletRequest request) {
        String resAccessUrl =null;
        try {
            for (MultipartFile file : files) {
                String fileUrl = dfsClient.uploadFile(file);
                //路径
                resAccessUrl = dfsClient.getResAccessUrl(fileUrl);
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("path",resAccessUrl);
        System.err.println(resAccessUrl);
        course_url=resAccessUrl;
        return map!=null?JsonData.buildSuccess(map):JsonData.buildError("服务端出错");
    }

    //获取课程的日志
    @RequestMapping("/getCourseClass")
    @ResponseBody
    public JsonData getCourseClass(){
        List<JsCourse> flag=service.getCourseClass();
        System.err.println(flag);
        return flag!=null?JsonData.buildSuccess(flag):JsonData.buildError("服务端错误");
    }
    //视频总数
    @RequestMapping("/sum")
    @ResponseBody
    public JsonData sum(){
        int sum = service.sum();
        return sum>=0?JsonData.buildSuccess(sum):JsonData.buildError("服务端错误");
    }
    //视频浏览总数
    @RequestMapping("/lookSum")
    @ResponseBody
    public JsonData lookSum(){
        int lookSum = service.lookSum();
        return lookSum>=0?JsonData.buildSuccess(lookSum):JsonData.buildError("服务端错误");
    }
    //视频收藏量
    @RequestMapping("/collectSum")
    @ResponseBody
    public JsonData collectSum(){
        int collectSum = service.collectSum();
        return collectSum>=0?JsonData.buildSuccess(collectSum):JsonData.buildError("服务端错误");
    }
    //视频点赞量
    @RequestMapping("/likeNum")
    @ResponseBody
    public JsonData likeNum(){
        int likeNum = service.likeNum();
        return likeNum>=0?JsonData.buildSuccess(likeNum):JsonData.buildError("服务端错误");
    }
    //课程上架量
    @RequestMapping("/courseStatus")
    @ResponseBody
    public JsonData courseStatus(){
        int courseStatus = service.courseStatus();
        return courseStatus>=0?JsonData.buildSuccess(courseStatus):JsonData.buildError("服务端错误");
    }

    //今天各个时间段的课程量
    @RequestMapping("/courseHourCount")
    public JsonData courseHourCount(){
        Map<String, Object> map = service.courseHourCount();
        return map!=null?JsonData.buildSuccess(map):JsonData.buildError("服务端错误");
    }

    //本周每天总数
    @RequestMapping("/courseWeekCount")
    public JsonData getCourseWeek(){
        Map<String, Object> courseWeek = service.courseWeekCount();
        return courseWeek.size() > 0 ? JsonData.buildSuccess(courseWeek) : JsonData.buildError("没有查到");
    }

    //本年每个月总数
    @RequestMapping("/courseDaycount")
    public JsonData getCourseDay(){
        Map<String, Object> courseDay = service.courseDaycount();
        return courseDay.size() > 0 ? JsonData.buildSuccess(courseDay) : JsonData.buildError("没有查到");
    }

}
